package com.cs.widgetvp.vm;


import androidx.lifecycle.MutableLiveData;

import com.cs.base.net.RetrofitHelp;
import com.cs.base.util.LogCat;
import com.cs.base.vm.BaseViewModel;
import com.cs.widgetvp.bean.JokeBean;
import com.cs.widgetvp.bean.PictureBean;
import com.cs.widgetvp.net.VPAPI;
import com.cs.widgetvp.net.VPUrl;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;


/**
 * Created by jcs on 2021/5/31.
 * Describe
 */
public class HomeViewModel extends BaseViewModel {

    public MutableLiveData<JokeBean> jokes = new MutableLiveData<>();
    public MutableLiveData<PictureBean> pictures = new MutableLiveData<>();

    public void getJoke() {
        showDialog.setValue(true);
        RetrofitHelp.getInstance()
                .request(VPUrl.url, VPAPI.class)
                .getJoke("utf-8", "json")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .safeSubscribe(new io.reactivex.rxjava3.core.Observer<JokeBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        addDisposable(d);
                        LogCat.d("RetrofitHelp", "onSubscribe:" + d.toString());
                    }

                    @Override
                    public void onNext(@NonNull JokeBean data) {
                        LogCat.d("RetrofitHelp", "onNext:" + data.toString());
                        jokes.setValue(data);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        LogCat.d("RetrofitHelp", "onSubscribe:" + e.getMessage());
                        showDialog.setValue(false);
                        error.setValue(e.getMessage());
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {
                        LogCat.d("RetrofitHelp", "onComplete:");
                        showDialog.setValue(false);
                    }
                });
    }


    public void getPicture(){
        showDialog.setValue(true);
        RetrofitHelp.getInstance()
                .request(VPUrl.url, VPAPI.class)
                .getPicture("fengjing", "json")
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .safeSubscribe(new io.reactivex.rxjava3.core.Observer<PictureBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        LogCat.d("RetrofitHelp", "onSubscribe:" + d.toString());
                    }

                    @Override
                    public void onNext(@NonNull PictureBean data) {
                        LogCat.d("RetrofitHelp", "onNext:" + data.toString());
                        pictures.setValue(data);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        LogCat.d("RetrofitHelp", "onSubscribe:" + e.getMessage());
                        showDialog.setValue(false);
                        error.setValue(e.getMessage());
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {
                        LogCat.d("RetrofitHelp", "onComplete:");
                        showDialog.setValue(false);
                    }
                });
    }

}
